import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    goodsList: [
      {
        id: 1,
        title: "低帮城市休闲户外鞋天然牛皮COOLMAX纤维",
        imgUrl:
          "https://ts1.tc.mm.bing.net/th/id/R-C.782a06bd3b2c2d0f5c921e3ba32b73a3?rik=bofy6ZxWsL2HKw&riu=http%3a%2f%2fa.vpimg2.com%2fupload%2fmerchandise%2f11647%2fNIKE-336617-104-1.jpg&ehk=MQxcRG2elCzCDT5Oi7fBIWkDHENXxx%2bV1V%2f%2fU3KgIu4%3d&risl=&pid=ImgRaw&r=0",
        price: 128.3,
        count: 2,
      },
      {
        id: 2,
        title:
          "冰丝阔腿裤女夏季薄款休闲防蚊夏天抖抖凉凉直筒显瘦垂感扎染裤子 水墨杏 小码90-120斤",
        imgUrl:
          "https://gw.alicdn.com/imgextra/i1/2218200743329/O1CN01B1QHHX1aSjYetCzr2_!!2218200743329.jpg_100x100Q75.jpg_.webp",
        price: 9.9,
        count: 1,
      },
      {
        id: 3,
        title:
          "碧春酒碧春伍53度酱香型贵州老酒高度白酒装宴请商务送礼 53%vol 500mL 12瓶 两箱装",
        imgUrl:
          "https://tse3-mm.cn.bing.net/th/id/OIP-C.VwSX9VuWkQLysYk0h71RngHaH_?cb=iwp2&rs=1&pid=ImgDetMain",
        price: 1636,
        count: 4,
      },
    ],
    showTip: false,
    // 0 啥也没有, 1 结算提示框alert, 2 确认框 confirm
    msg: {
      text: "",
      type: 0,
      showCancel: false,
      showSubmit: false,
    },
  },
  getters: {
    allPrice(state) {
      return state.goodsList.reduce(
        (res, item) => item.count * item.price + res,
        0
      );
    },
  },
  mutations: {
    // 改变数量
    changeCount(state, params) {
      // 结构传入的参数
      const { n, index } = params;
      // 判断参数
      if (state.goodsList[index].count === 1 && n < 0) {
        // 商品数量为0时, 并且还要继续减少商品
        // 展示提示
        state.showTip = true;
        // 定义 提示信息
        state.msg = {
          index,
          text: "是否删除该商品?",
          type: 2,
          showCancel: true,
          showSubmit: true,
        };
        return;
      }
      // 修改count
      state.goodsList[index].count += n;
    },
    // 结算
    finished(state) {
      // 显示提示窗口
      state.showTip = true;
      state.msg = {
        text: "结算成功",
        type: 1,
        showCancel: false,
        showSubmit: true,
      };
    },
    // 关闭提示
    closeTip(state) {
      state.showTip = false;
    },
    delGood(state) {
      state.goodsList.splice(state.msg.index, 1);
    },
  },
  actions: {},
  modules: {},
});
